package com.btks.jswater.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.btks.jswater.entity.*;
import com.btks.jswater.mapper.ReservoirInfoMapper;
import com.btks.jswater.mapper.RiverInfoMapper;
import com.btks.jswater.service.IReservoirInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ZhangKewen
 * @since 2021-12-22
 */
@Service
public class ReservoirInfoServiceImpl extends ServiceImpl<ReservoirInfoMapper, ReservoirInfo> implements IReservoirInfoService {
    @Resource
    private RiverInfoMapper riverInfoMapper;

    public List<WaterRelationPoint> getRealtion(String id) {
        return riverInfoMapper.getShuizha(id);
    }

    public List<WaterDirectonJson> getDirections(String id) {
        List<WaterDirections> water_dic = riverInfoMapper.getDirections(id);
        List<WaterDirectonJson> water_dic_json = new ArrayList<>();
        JSONObject res = new JSONObject();
        for (int i = 0; i < water_dic.size(); i++) {
            ArrayList res1 = new ArrayList<>();
            Object json = JSONObject.parse(water_dic.get(i).getJson());
            JSONObject jsonObject = JSONObject.parseObject(json.toString());
            String r = jsonObject.getString("features");
            List<String> userList = JSONArray.parseArray(r, String.class);
            for (int j = 0; j < userList.size(); j++) {
                JSONObject jsonObject1 = JSONObject.parseObject(userList.get(j));
                String geo = jsonObject1.getString("geometry");
                JSONObject jsonObject2 = JSONObject.parseObject(geo);
                String cor = jsonObject2.getString("coordinates");
                String[] cors = cor.split(",");
                float x = Float.parseFloat(cors[0].substring(1, cors[0].length()));
                float y = Float.parseFloat(cors[1].substring(0, cors[1].length() - 1));
                float[] coordinates = {x, y};
                res1.add(coordinates);
                System.out.println(res1);
                System.out.println(res1);
            }
            WaterDirectonJson water_dic_json1 = new WaterDirectonJson();
            water_dic_json1.setLines(res1);
            if(i%2==0){
                water_dic_json1.setStatus(1);
            }else{
                water_dic_json1.setStatus(0);
            }

            water_dic_json.add(water_dic_json1);
        }
        return water_dic_json;
    }

    public WaterPointRelationTable getPointTable(String name) {
        return getTableIterations(name,1);
    }

    public WaterPointRelationTable getTableIterations(String name , int code) {
        List<WaterRelationPoint> waterTable = riverInfoMapper.getPointCount();
        WaterPointRelationTable waterPointTable = new WaterPointRelationTable();
        for (WaterRelationPoint waterRelationPoint : waterTable) {
            String name1= waterRelationPoint.getName();
            if(name1.equals(name)){
                if (waterRelationPoint.getCode() == code) {
                    waterPointTable.setName(waterRelationPoint.getStname());
                    waterPointTable.setX(waterRelationPoint.getX());
                    waterPointTable.setY(waterRelationPoint.getY());
                    //获取当天平均流量数据
                    Calendar calendar = Calendar.getInstance();
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    String date = formatter.format(calendar.getTime());
                    float count = riverInfoMapper.getTableCountInfo("2022-01-01","2022-01-01",waterRelationPoint.getStcd());
                    if(count!=0){
                        waterPointTable.setStatus(1);
                    }else{
                        waterPointTable.setStatus(0);
                    }
                    waterPointTable.setCount(count);
                    waterPointTable.setStcd(waterRelationPoint.getStcd());
                    //获取数据的最新更新时间
                    String updateTime = riverInfoMapper.updateTime(waterRelationPoint.getStcd());
                    waterPointTable.setUpdateTime(updateTime);
                    List<WaterPointRelationTable> tableChild = new ArrayList<>();
                    if (!Objects.equals(waterRelationPoint.getNcode(), "") && waterRelationPoint.getNcode() != null) {
                        String[] ncode = waterRelationPoint.getNcode().split(",");
                        if (ncode.length != 0) {
                            for (int i = 0; i < ncode.length; i++) {
                                int k = Integer.parseInt(ncode[i]);
                                WaterPointRelationTable table1 = getTableIterations(name,k);
                                tableChild.add(table1);
                            }
                            waterPointTable.setMaps(tableChild);
                        } else {
                            tableChild.add(null);
                        }
                    }
                    return waterPointTable;
                }
            }
        }
        return waterPointTable;
    }

    /**
     * 获取渠系关系图中站点详细信息
     * 参数位id,数据库中stcode
     */
    public List<WaterMoniData> getStInfo(String id) {
        return riverInfoMapper.getSiteInfo(id);
    }


    /**
     * 根据时间获取渠系关系图中站点详细信息
     * 参数为起止时间和测站编号
     */

    public List<WaterMoniData> getInfoByTime(String stime,String etime,String scode){
        return riverInfoMapper.getInfoByTime(stime,etime,scode);
    }

    /**
     * 获取渠系关系表中附带的流量
     * 参数为起止时间以及监测站编号，默认传入当天
     */

    public float getTableCountInfo(String stime,String etime,String scode) {
        return riverInfoMapper.getTableCountInfo(stime,etime,scode);
    }
    /**
     * 时间段内的用水查询
     * 参数为起止时间以及水库编号，默认传入当天
     */

    public List<Map> getMoniData(String stime, String etime, String scode) {
        return riverInfoMapper.getMoniData(scode,stime,etime);
    }
    /**
     * 时间段内水库龙口查询
     * 参数为起止时间以及水库编号
     */
    public List<WaterMoniData> getSumDataByDate(String stime, String etime, String scode) {
        return riverInfoMapper.getShuikuData(scode,stime,etime);
    }
}
